﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using ICeFramework.Common.DataLayer.Enums;

namespace ICeFramework.Common.DataLayer
{
    [Serializable, DataContract]
    public class EntityOrder
    {
        [DataMember]
        private EntityColumn _column;
        public EntityColumn Column
        {
            get { return _column; }
            set { _column = value; }
        }

        [DataMember]
        private EntityOrderDirection _direction;
        public EntityOrderDirection Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        [DataMember]
        internal EntityOrder _innerOrder = null;

        public EntityOrder(EntityColumn column, EntityOrderDirection direction)
        {
            _column = column;
            _direction = direction;
        }

        public static EntityOrder operator +(EntityOrder a, EntityOrder b)
        {
            if ((object)a != null && (object)b != null)
            {
                a._innerOrder = b;
                return a;
            }
            return null;
        }

        public static EntityOrder operator &(EntityOrder a, EntityOrder b)
        {
            if ((object)a != null && (object)b != null)
            {
                a._innerOrder = b;
                return a;
            }
            return null;
        }

        public override string ToString()
        {
            string order = string.Format("{0} {1}", _column.Name, _direction);
            EntityOrder inner = _innerOrder;
            while (inner != null)
            {
                order += string.Format(", {0} {1}", inner.Column.Name, inner.Direction);
                inner = inner._innerOrder;
            }
            return order;
        }

    }
}
